package com.whmnx.chitchat.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonRecyclerAdapter;
import com.cy.cyflowlayoutlibrary.FlowLayoutAdapter;
import com.cy.cyflowlayoutlibrary.FlowLayoutScrollView;
import com.tencent.qcloud.tim.uikit.utils.DateTimeUtil;
import com.whmnx.chitchat.R;
import com.whmnx.chitchat.bean.BaseBean;
import com.whmnx.chitchat.bean.DynaminBean;
import com.whmnx.chitchat.bean.LoginBean;
import com.whmnx.chitchat.constants.Constants;
import com.whmnx.chitchat.http.CallBack;
import com.whmnx.chitchat.http.HttpClient;
import com.whmnx.chitchat.main.LoginActivity;
import com.whmnx.chitchat.main.RegisterActivity;
import com.whmnx.chitchat.utils.ImageUtil;
import com.whmnx.chitchat.utils.SPUtil;
import com.whmnx.chitchat.utils.TimeUtils;
import com.whmnx.chitchat.utils.UItils;
import com.whmnx.chitchat.view.CommonDialog;
import com.whmnx.chitchat.view.DynamicInputTextMsgDialog;
import com.whmnx.chitchat.view.FlowLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author : huang
 * date   : 2019/10/21 0021 15:06
 * version: 1.0
 * @author Administrator
 */
public class DynamicAdapter extends CommonRecyclerAdapter<DynaminBean.ForumListBean> {
    private Context context;
    private List<Boolean> alreadyLikeList = new ArrayList<>();
    private List<String> lists = new ArrayList<>();
    private ImageView view;

    private ReplyListener mReplyListener;
    private FlowLayoutAdapter flowLayoutAdapter;
    private int pos;
    private int pos1;
    private DynamicInputTextMsgDialog dynamicInputTextMsgDialog;


    public void setReplyListener(ReplyListener replyListener) {
        mReplyListener = replyListener;
    }


    public interface ReplyListener {
      //  void commentReplyTopUser(DynamicCommentBean item, int position);

        void commentReplyUser(String item, String  forumComentId,DynaminBean.ForumListBean.CommentBean commentBean,int position);

     //   void dianz(DynamicCommentBean item, int position);
    }


    public DynamicAdapter(Context context) {
        super(context, R.layout.item_list_dynamic);
        this.context = context;
    }


    @Override
    public void onUpdate(BaseAdapterHelper helper, DynaminBean.ForumListBean item, int position) {
        pos1 = position;

        ImageUtil.setImage(helper.getView(R.id.civ_personal_head_image), (String) item.getForum().getUserInfo_HeadImg());
        helper.setText(R.id.tv_name, (String) item.getForum().getUserInfo_NickName());
     //   long t = UItils.dateToStamp(item.getForum().getForum_CreateTime());
     //   helper.setText(R.id.tv_time, DateTimeUtil.getTimeFormatText(new Date(t*1000)));

        helper.setText(R.id.tv_time, TimeUtils.getFriendlyTimeSpanByNow(item.getForum().getForum_CreateTime()));
      //  helper.setText(R.id.tv_time, (String) item.getForum().getForum_CreateTime());
        helper.setText(R.id.tv_dynamic_detail, (String) item.getForum().getForum_Content());
     //   helper.setText(R.id.tv_msg, item.getForum_CommentNum() + "");
      //  helper.setText(R.id.tv_like,  item.getForum_PraiseNum() + "");
         GridView view = helper.getView(R.id.rv_dynamic_photos);
         ImageView imageView = helper.getView(R.id.iv_like);
         TextView tv_like = helper.getView(R.id.tv_like);
         if (item.getForum().getUserInfo_ID().equals(SPUtil.getString(Constants.USERID))){
             helper.setVisible(R.id.iv_del, View.VISIBLE);
         }else {
             helper.setVisible(R.id.iv_del, View.GONE);
         }

         if (item.getLikes().size() != 0) {
             //FlowLayout flowLayout =  helper.getView(R.id.flow);
             helper.setVisible(R.id.ib_like2, View.VISIBLE);
             helper.setVisible(R.id.flow,View.VISIBLE);
         //   RecyclerView rlLike = helper.getView(R.id.rl_like);
        //    rlLike.setItemAnimator(new DefaultItemAnimator());
         //   LikeAdapter likeAdapter = new LikeAdapter(context);
        //    rlLike.setAdapter(likeAdapter);
         //   likeAdapter.addAll((List<DynaminBean.ForumListBean.LikesBean>) item.getLikes());
             lists.clear();
             for (int i = 0; i < item.getLikes().size(); i++) {
                 lists.add(item.getLikes().get(i).UserInfo_NickName);
             }
              flowLayoutAdapter = new FlowLayoutAdapter<String>(lists) {
                 @Override
                 public void bindDataToView(ViewHolder holder, int position, String bean) {
                     pos1 = position;
                     holder.setText(R.id.tv_name,bean);
                 }

                 @Override
                 public void onItemClick(int position, String bean) {
                      // remove(position);
                 }

                 @Override
                 public int getItemLayoutID(int position, String bean) {
                     return R.layout.item_list_like2;
                 }
             };
             ((FlowLayoutScrollView)helper.getView(R.id.flow)).setAdapter(flowLayoutAdapter);
        }else {
            helper.setVisible(R.id.ib_like2, View.GONE);
            helper.setVisible(R.id.flow, View.GONE);
            //线条
            helper.setVisible(R.id.view, View.GONE);
        }

        RecyclerView rlComment = helper.getView(R.id.rl_comment);
        if (item.getComment().size() == 0){
            rlComment.setVisibility(View.GONE);
            helper.setVisible(R.id.view, View.GONE);
        }else {
            rlComment.setVisibility(View.VISIBLE);
            helper.setVisible(R.id.view, View.VISIBLE);
            rlComment.setLayoutManager(new LinearLayoutManager(context));
            rlComment.setItemAnimator(new DefaultItemAnimator());
            CommentAdapter commentAdapter = new CommentAdapter(context);
            rlComment.setAdapter(commentAdapter);
            commentAdapter.addAll((List<DynaminBean.ForumListBean.CommentBean>) item.getComment());

            commentAdapter.setReplyListener(new ReplyListener() {
                @Override
                public void commentReplyUser(String item2, String forumComentId, DynaminBean.ForumListBean.CommentBean commentBean, int position) {
                   // mReplyListener.commentReplyUser(item,forumComentId,commentBean ,position);
                    if (null == dynamicInputTextMsgDialog)dynamicInputTextMsgDialog = new DynamicInputTextMsgDialog(context);
                    dynamicInputTextMsgDialog.setShowType();
                    dynamicInputTextMsgDialog.setOnSendClickListener(new DynamicInputTextMsgDialog.OnSendClickListener() {
                        @Override
                        public void onSendClick(String text) {
                            Map<String, Object> params = new HashMap<>();
                            params.put("UserID", SPUtil.getString(Constants.USERID));
                            params.put("ForumID", item2);
                            params.put("ForumCommentID", forumComentId);
                           /* if (TextUtils.isEmpty(commentBean.ForumComment_NikeName)){
                                params.put("ForumID", item2);
                                params.put("ForumCommentID", forumComentId);
                            }else {
                                params.put("ForumID", item2);
                                params.put("ForumCommentID", forumComentId);
                            }*/
                            params.put("Content", text);

                            HttpClient.post2(context, Constants.POSTADDFORUMCOMMENT, params,new CallBack<String>() {
                                @Override
                                public void onSuccess(String result) {
                                    DynaminBean.ForumListBean.CommentBean commentBean2 = new DynaminBean.ForumListBean.CommentBean();
                                    commentBean2.UserInfo_NickName = SPUtil.getString(Constants.NICKNAME);
                                    commentBean2.ForumComment_NikeName = commentBean.UserInfo_NickName;
                                  /*  if (TextUtils.isEmpty(commentBean.ForumComment_NikeName)){
                                    }else {
                                        commentBean2.UserInfo_NickName = SPUtil.getString(Constants.NICKNAME);
                                        commentBean2.ForumComment_NikeName = commentBean.ForumComment_NikeName;
                                    }**/

                                    commentBean2.ForumComment_Content = text;
                                    item.getComment().add(commentBean2);
                                    notifyDataSetChanged();
                                }
                            });
                        }
                    });
                }
            });

        }

        if (item.getLikes().size() == 0 && item.getComment().size() ==0 ){
            helper.setVisible(R.id.ll, View.GONE);
        }else {
            helper.setVisible(R.id.ll, View.VISIBLE);
            helper.setVisible(R.id.view, View.VISIBLE);
        }



        helper.getView(R.id.iv_msg).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null == dynamicInputTextMsgDialog)dynamicInputTextMsgDialog = new DynamicInputTextMsgDialog(context);
                dynamicInputTextMsgDialog.setShowType();
                dynamicInputTextMsgDialog.setOnSendClickListener(new DynamicInputTextMsgDialog.OnSendClickListener() {
                    @Override
                    public void onSendClick(String text) {
                        Map<String, Object> params = new HashMap<>();
                        params.put("UserID", SPUtil.getString(Constants.USERID));
                        params.put("ForumID", item.getForum().getForum_ID());
                        params.put("ForumCommentID", "");

                        params.put("Content", text);

                        HttpClient.post2(context, Constants.POSTADDFORUMCOMMENT, params,new CallBack<String>() {
                            @Override
                            public void onSuccess(String result) {
                                DynaminBean.ForumListBean.CommentBean commentBean2 = new DynaminBean.ForumListBean.CommentBean();
                                commentBean2.UserInfo_NickName = SPUtil.getString(Constants.NICKNAME);
                                commentBean2.ForumComment_NikeName = "";
                                commentBean2.ForumComment_Content = text;
                                item.getComment().add(commentBean2);
                                notifyDataSetChanged();
                            }
                        });
                    }
                });




              /* if (mReplyListener != null) {
                    mReplyListener.commentReplyUser(item.getForum().getForum_ID(),"", (DynaminBean.ForumListBean.CommentBean) item.getComment(),position);
                }*/
            }
        });

        /**
         * 点赞
         */
        helper.getView(R.id.iv_like).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("myFragment----updata---", position + "");
                   // mReplyListener.commentReplyUser(item.getForum().getForum_ID(),"dian" ,position);
                    Map<String, Object> params = new HashMap<>();
                    params.put("UserID", SPUtil.getString(Constants.USERID));
                    params.put("ForumID", item.getForum().getForum_ID());
                    HttpClient.get(context, Constants.GETFORUMLIKES, params,new CallBack<BaseBean>() {   //点赞
                        @Override
                        public void onSuccess(BaseBean result) {
                            if (result.getStatus() == 1){
                                String msg  =  result.getMessage();
                                String nickName = SPUtil.getString(Constants.NICKNAME);
                                List<DynaminBean.ForumListBean.LikesBean> likes = item.getLikes();
                                if (msg.contains("取消")){
                                    for (DynaminBean.ForumListBean.LikesBean like : likes) {
                                        if (like.UserInfo_NickName.equals(nickName)){
                                            likes.remove(like);
                                        }
                                    }
                                }else {
                                    DynaminBean.ForumListBean.LikesBean likesBean = new DynaminBean.ForumListBean.LikesBean();
                                    likesBean.UserInfo_NickName = nickName;
                                    likes.add(likesBean);
                                }

                                notifyDataSetChanged();
                            }
                        }
                    });

                }
               /* Map<String, Object> params = new HashMap<>();
                params.put("UserID", SPUtil.getString(Constants.USERID));
                params.put("ForumID", item.getForum().getForum_ID());
                HttpClient.get(context, Constants.GETFORUMLIKES, params,new CallBack<BaseBean>() {
                    @Override
                    public void onSuccess(BaseBean result) {
                        if (result.getStatus() == 1){
                            if (result.getMessage().equals("点赞成功")){
                              //  lists.add(SPUtil.getString(Constants.NICKNAME));
                               // flowLayoutAdapter.add(lists);
                            }else {
                             //   lists.remove(SPUtil.getString(Constants.NICKNAME));
                                flowLayoutAdapter.remove(pos);
                              //  flowLayoutAdapter.remove(pos);
                            }
                          //  flowLayoutAdapter.notifyDataSetChanged();
                        }
                    }
                });*/
        });

        helper.getView(R.id.iv_del).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s2 = "确定删除吗?";
                new CommonDialog(context, R.style.dialog, s2, (dialog, confirm) -> {
                    if (confirm) {
                        Map<String, Object> params = new HashMap<>();
                        params.put("UserID",SPUtil.getString(Constants.USERID));
                        params.put("ForumID",item.getForum().getForum_ID());
                        HttpClient.get(context, Constants.GETDELETEFORUM, params,new CallBack<LoginBean>() {
                            @Override
                            public void onSuccess(LoginBean result) {
                                getData().remove(position);
                                notifyItemRemoved(position);
                            }
                        });
                    }
                    dialog.dismiss();
                }).show();
            }
        });

        //9宫格图片
        List<String> list = new ArrayList<>();
        FansImagesAdpter fansImagesAdpter = new FansImagesAdpter(context, list);
        view.setAdapter(fansImagesAdpter);
        if (!TextUtils.isEmpty(item.getForum().getForum_Imgs()) && item.getForum().getForum_Imgs().contains(",")) {
            String [] split = item.getForum().getForum_Imgs().split(",");
            list.addAll(Arrays.asList(split));
            fansImagesAdpter.notifyDataSetChanged();
        }else {
            if (TextUtils.isEmpty(item.getForum().getForum_Imgs())){
                list.clear();
                fansImagesAdpter.notifyDataSetChanged();
            }else {
                list.add(item.getForum().getForum_Imgs());
                fansImagesAdpter.notifyDataSetChanged();
              //  FansImagesAdpter fansImagesAdpter = new FansImagesAdpter(context, list);
              //  view.setAdapter(fansImagesAdpter);
            }
        }
    }
}

